<template>
  <view class="container">
    <view class="top-bar"></view>
    <view class="user-container">
      <view class="user-panel">
        <navigator url="/pages/login/login">
          <button type="primary" size="mini">微信一键登录</button>
        </navigator>
      </view>
      <view class="user-panel">
        <button type="primary" size="mini" @click="upload">上传图片</button>
      </view>
      <view class="user-panel">
        <button type="primary" size="mini" @click="upload_multi">多图片上传</button>
        <button type="primary" size="mini" @click="upload_for">小程序循环上传</button>
      </view>
      <view class="user-panel">
        <button type="primary" size="mini" @click="test">测试请求</button>
      </view>
      <view class="user-panel">
        <button type="primary" size="mini" @click="getUserProfile">小程序登录</button>
      </view>
      <view class="user-panel">
        <button type="primary" size="mini" @click="chat">chat</button>
      </view>
    </view>
  </view>
</template>

<script>
  import { getMe, uploadImage, MultiuploadImage, wxAppLogin } from "@/common/api"

  export default {
    data() {
      return {}
    },
    methods: {
      upload() {
        uni.chooseImage({
          count: 1,
          sizeType: ["compressed"],
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths;
            uploadImage(tempFilePaths[0]).then(res => {
              console.log(res)
            })
          }
        })
      },
      upload_multi() {
        uni.chooseImage({
          sizeType: ["compressed"],
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths
            let imgs = tempFilePaths.map((value, index) => {
              return { name: index + 1, uri: value }
            })
            console.log("imgs", imgs)
            MultiuploadImage(imgs).then(res => {
              console.log(res)
            })
          }
        })
      },
      upload_for() {
        uni.chooseImage({
          sizeType: ["compressed"],
          success: (chooseImageRes) => {
            const tempFilePaths = chooseImageRes.tempFilePaths
            for (let item in tempFilePaths) {
              console.log("item", tempFilePaths[item])
              uploadImage(tempFilePaths[item]).then(res => {
                console.log(res.data)
              })
            }
          }
        })
      },
      test() {
        getMe().then(res => {
          console.log(res.data)
        })
      },
      getUserProfile() {
        wxAppLogin().then(res => {
          uni.setStorageSync('token', res.data.access_token)
          uni.setStorageSync('refresh_token', res.data.refresh_token)
          uni.setStorageSync('loginState', true)
          uni.switchTab({
            url: '/pages/index/index'
          })
        })
      },
      chat() {
        uni.navigateTo({
          url: '/pages/chat/chat'
        })
      }
    },
  }
</script>

<style scoped>
  @import "test.css";
</style>
